package com.wm.eduService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.commonutils.R;
import com.wm.commonutils.result.ResultCode;
import com.wm.eduService.entity.EduCourse;
import com.wm.eduService.entity.vo.CourseInfoVo;
import com.wm.eduService.entity.vo.CoursePublishVo;
import com.wm.eduService.entity.vo.CourseQuery;
import com.wm.eduService.enums.CourseStatusEnum;
import com.wm.eduService.service.EduCourseService;
import com.wm.servicebase.exceptionhandle.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author sshdg
 * @since 2020-10-01
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduService/course")
@Api(tags = "课程管理")
public class EduCourseController {
    @Autowired
    EduCourseService courseService;

    @ApiOperation("添加课程信息")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfo){

        String courseId = courseService.saveCourseInfo(courseInfo);
        return R.ok().data("courseId",courseId);
    }

    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfoByCourseId(courseId);
        return R.ok().data("courseInfo",courseInfoVo);
    }

    @ApiOperation("修改课程信息")
    @PutMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfo){
        courseService.updateCourse(courseInfo);
        return R.ok();
    }


    @ApiOperation("根据课程id查询课程确认信息")
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);

        return R.ok().data("coursePublishInfo",coursePublishVo);
    }


    @ApiOperation("课程发布")
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus(CourseStatusEnum.NORMAL.getDesc());

        boolean b = courseService.updateById(course);
        if (!b){
            throw GuliException.from(ResultCode.UPDATE_ERROR);
        }
        return R.ok();
    }

    @ApiOperation("分页查询课程(带查询条件)")
    @PostMapping("pageCourse/{current}/{limit}")
    public R pageCourse(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody CourseQuery courseQuery){

        Page<EduCourse> page = new Page<>(current, limit);

        Map<String, Object> map = courseService.pageQueryCourse(page, courseQuery);

        // 总记录数
        long total = (long) map.get("total");
        // 分页查询的数据
        List<EduCourse> records = (List<EduCourse>) map.get("records");
        
        return R.ok().data(map);
    }



    @ApiOperation("删除课程信息")
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId){

        courseService.deleteCourse(courseId);
        return R.ok();
    }

}

